package com.zzh.tools.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2020-07-27
 * @desc 处理bean；类
 */
public class ToolBean {

    /**
     * bean类假数据使用
     *
     * @param cla    bean class
     * @param isNull 里面的数据可不可以是null
     * @return 返回创建的对象
     */
    public static <T> T createBean(Class<T> cla, boolean isNull) {
        T object = null;
        if (isNull) {
            try {
                return cla.newInstance();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
        Field[] fields = cla.getDeclaredFields(); //获取实体类的所有属性，返回Field数组
        try {
            object = cla.newInstance();
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                String type = field.getGenericType().toString();
                Method method = object.getClass().getMethod("set" + getMethodName(field.getName()), field.getType());
                if (type.contains(".List")) {
                    Type[] genericParameterTypes = method.getGenericParameterTypes();
                    if (genericParameterTypes.length > 0) {
                        if (genericParameterTypes[0] instanceof ParameterizedType) {
                            Type[] actualTypeArguments = ((ParameterizedType) genericParameterTypes[0]).getActualTypeArguments();
                            if (actualTypeArguments.length > 0) {
                                Type t1 = actualTypeArguments[0];
                                if (t1.toString().contains(".bean.")) {
                                    System.out.println("不做处理");
                                }
                            }
                        }
                    }
                } else if (type.contains(".bean.")) {
                    method.invoke(object, createBean(field.getType(), false));
                } else if (type.contains(".String")) {
                    method.invoke(object, "");
                } else if (type.contains(".Double") || type.contains("double")) {
                    method.invoke(object, 0.0);
                } else if (type.contains(".Integer") || type.contains("int")) {
                    method.invoke(object, 0);
                } else if (type.contains(".Boolean") || type.contains("boolean")) {
                    method.invoke(object, false);
                } else if (type.contains(".Float") || type.contains("float")) {
                    method.invoke(object, 0f);
                } else if (type.contains(".Long") || type.contains("long")) {
                    method.invoke(object, 0L);
                } else if (type.contains(".Short") || type.contains("short")) {
                    method.invoke(object, (short) 0);
                } else if (type.contains(".Byte") || type.contains("byte")) {
                    method.invoke(object, (byte) 0);
                } else {
                }
            }

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return object;
    }

    /**
     * bean类假数据使用
     *
     * @param cla    bean class
     * @param isNull 里面的数据可不可以是null
     * @return 返回创建的对象
     */
    public static <T> List<T> createBeans(Class<T> cla, boolean isNull, int count) {
        List<T> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            list.add(createBean(cla, isNull));
        }
        return list;
    }

    /**
     * 将属性名称的首字母变成大写
     */
    public static String getMethodName(String fieldName) {
        byte[] bytes = fieldName.getBytes();
        bytes[0] = (byte) (bytes[0] - 'a' + 'A');
        return new String(bytes);
    }


}
